<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Category extends Frontend_Controller
{

	public function __construct()
	{
		parent::__construct();

		$this->template->set_layout('listing_layout');
		//--PARTIALS--//
		$this->template->set_partial('header', 'header/headerTop');
        $this->template->set_partial('footer', 'footer/footer');
		$this->load->model(array('browse/browse_model'));
		$this->load->model('gallery_model');
		$this->load->library('pagination');
	}



	function search($data, $offset , $limit ){

		$locID = $data["tmpLOC"]; $catID = $data["tmpCAT"];
		
		
		//print $offset.'-----'.$limit; die();
		
		$data['page'] = "FRONT";
		//--LOCATION --//
		if( isset($locID) && $locID !=0 ){
			$data['locData'] = $this->location_model->getPK($locID);
			$data['location'] = $data['locData']['name'];
			
		}else{
			$data['locData'] = array('name' => config_item('country'));
			
			
		}

		//--CATEGORY --//
		if( isset($catID) && $catID !=0 ){

			$data['catData'] = $this->category_model->getPK($catID);


		}else{
			$data['locData'] = array('name' => 'All');
		}

		$data['category'] = $this->category_model->getChild($catID,20,TRUE) ;



		if(!$data['category']) $data['category'] = $this->category_model->getChild($catID,20,FALSE) ;


		//--BREADCRUMB --//
		$breadcrumb = $this->category_model->breadcrumb($catID);
		
		foreach($breadcrumb as $br){
			
			$this->template->set_breadcrumb($br['name'], $br['slug']);
		}


		//--META DATA---//
		$this->template->title($data['catData']['meta_title'], setting('web_title'));
		$this->template->set_metadata('keywords', $data['catData']['meta_keyword']);
		$this->template->set_metadata('description', $data['catData']['meta_description']);
		$this->template->set_metadata('author', setting('google_plus'));


		$seg_cat = $this->uri->segment(2);

		
		
		$locD = $data['locData']['name'];
		//if($this->uri->segment(3)){
			//$offset = http_build_query($_GET);
		//}
		
		//print $offset; die();
		

		$config['total_rows'] = $this->browse_model->countAlllisting($catID);
		$config['base_url'] = base_url() . url_title($locD).'/'. $seg_cat;
		//$config['base_url'] = base_url() . 'United-States/' . $seg_cat.'?page'.http_build_query($_GET);

		$cur =base_url()."United-States/'.$seg_cat";
		$cur = '<a href="'. $cur   .'">&laquo; First</a>';
		$config['full_tag_open'] = '<ul class="pagination">';
		$config['full_tag_close'] = '</ul><!--pagination-->';
		$config['first_link'] = '&laquo; First';
		$config['first_tag_open'] = '<li class="prev page" style="display:inline">';
		$config['first_tag_close'] = '</li>';
		$config['last_link'] = 'Last &raquo;';
		$config['last_tag_open'] = '<li class="next page" style="display:inline">';
		$config['last_tag_close'] = '</li>';
		$config['next_link'] = 'Next &rarr;';
		$config['next_tag_open'] = '<li class="next page" style="display:inline">';
		$config['next_tag_close'] = '</li>';
		$config['prev_link'] = '&larr; Previous';
		$config['prev_tag_open'] = '<li class="prev page" style="display:inline">';
		$config['prev_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><a href="">';
		$config['cur_tag_close'] = '</a></li>';
		$config['num_tag_open'] = '<li class="page">';
		$config['num_tag_close'] = '</li>';
		$config['display_pages'] = FALSE;
		
		if( isset($locID) && ($catID) == TRUE ){
		$this->pagination->initialize($config);
		
		}
		//--GET POST DATA --//
		$data['post'] = $this->browse_model->getPost($locID,$catID,$offset,$limit);


		//$pCat = $this->mahana_hierarchy->get_ancestors($catID);

		//$cCat = $this->mahana_hierarchy->get_children($catID);

		//var_dump($pCat);die();


		if($data['catData']['parent_id'] == 0){
			$cCat = $this->mahana_hierarchy->get_children($catID);
			$shows = NULL;
			$pSQL='';
			for($i=0; $i < count($cCat); $i++){
				$pSQL .= "cat_id = ".$cCat[$i]['id']." OR ";
				//var_dump($pSQL);
				//die("hello");
			}

			$len=strlen($pSQL);
			$pos=strrpos($pSQL, 'OR');
			$pSQL= substr($pSQL,0,$pos);

			/*WAITING FOR FILTERING
			 $sql ="SELECT * FROM gallery
			WHERE ($pSQL) and (term='banner') or (shows=2) limit 3";
			*/
			$sql ="SELECT * FROM gallery
			WHERE ($pSQL) and (term='banner') limit 3";

			$query = $this->db->query($sql);
			if($query){
				$result = $query->result();

			}else{

				$result = FALSE;
			}


			$data['bannerInner'] = $result;

		}else{
			$pCat = $this->mahana_hierarchy->get_ancestors($catID);
			$pSQL='';
			for($i=0; $i < count($pCat); $i++){
				$pSQL .= "cat_id = ".$pCat[$i]['id']." OR ";
				//var_dump($pSQL);
				//die("hello");
			}

			$len=strlen($pSQL);
			$pos=strrpos($pSQL, 'OR');
			$pSQL= substr($pSQL,0,$pos);


			$sql ="SELECT * FROM gallery
			WHERE ($pSQL) and (term='banner') limit 3";
			//print $sql;die();
				
			$query = $this->db->query($sql);
			if($query){
				$result = $query->result();
					
			}else{
					
				$result = FALSE;
			}
				
			$data['bannerInner'] = $result;

		}
		
		
		
		
		$this->load->library('googlemaps');
		//$config['center'] = '37.4419, -122.1419';
		$config['zoom'] = 'auto';		$this->googlemaps->initialize($config);
		
		//print base_url();
		//die();
		
		$m=1;
		if($data['post']){
			foreach ($data['post'] as $map){
				$img_path = base_url()."assets/img/map_icons/numeric/red".$m.".png";
				$long_lat = $map->lat.','.$map->long;
				$marker = array();
				$marker['icon'] = $img_path;
				$marker['position'] = $long_lat;
				$marker['infowindow_content'] = "<strong>$map->title</strong>"."</br>"."$map->address_1"." $map->address_1";
				$marker['animation'] = 'DROP';
				$this->googlemaps->add_marker($marker);
				$m++;
			}
			$data['map'] = $this->googlemaps->create_map();
		}

		//var_dump($data['bannerInner']);
		//die();

		$this->template->build('listing',$data);
	}

	function autocomplete(){
			
		$search = $_GET['term'];

		$query = $this->db->query("SELECT id,name FROM `category` WHERE `name` LIKE '%$search%' and id!=0 ORDER BY name ASC limit 15") or die('Something went wrong');
		$result = $query->result();


		$row_set = array();

		foreach ($result as $row1){
			$row = array(
					'label'	=> stripslashes($row1->name),
					'value'	=> 	(int)$row1->id
			);
			$row_set[] = $row;//build an array
		}

		echo json_encode($row_set);//format the array into json data



	}

	function all()
	{
		$data['mode'] = "CATEGORY";
		
		$data['category'] = $this->category_model->getCetegory(0,10,FALSE);
		$data['state'] =  $this->location_model->getLocation(0,99);
		$data['location'] =  config_item('country');

		
		//BREADCRUMB//
		$this->template->set_breadcrumb('Home', '/');
		$this->template->set_breadcrumb('Category', 'category/all');
		$this->template->set_breadcrumb('All');
		
		$this->template->set_partial('header','header/headerTop');
		$this->template->set_layout('full_layout');
		$this->template->build('all', $data);
	}

	function getChild($parentID){
		header('Content-Type: application/x-json; charset=utf-8');
		echo(json_encode($this->category_model->getChildbyparent($parentID)));
		die();
	}
	
	function getKeywords($childID){
		header('Content-Type: application/x-json; charset=utf-8');
		echo(json_encode($this->category_model->getKeywordbysubCategory($childID)));
		die();
	}
	
}